﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Framework.Message.SMS.SGIP
{
    public class ByteIntConvert
    {
        /// <summary>
        /// 转换整形数据网络次序的字节数组
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static byte[] UInt2Bytes(uint i)
        {
            byte[] t = BitConverter.GetBytes(i);
            byte b = t[0];
            t[0] = t[3];
            t[3] = b;
            b = t[1];
            t[1] = t[2];
            t[2] = b;
            return (t);
        }

        /// <summary>
        /// 返回字节数组代表的整数数字，4个数组
        /// </summary>
        /// <param name="bs"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        public static uint Bytes2UInt(byte[] bs, int startIndex)
        {
            byte[] t = new byte[4];
            for (int i = 0; i < 4 && i < bs.Length - startIndex; i++)
            {
                t[i] = bs[startIndex + i];
            }
            byte b = t[0];
            t[0] = t[3];
            t[3] = b;
            b = t[1];
            t[1] = t[2];
            t[2] = b;
            return (BitConverter.ToUInt32(t, 0));
        }

        /// <summary>
        /// 没有指定起始索引
        /// </summary>
        /// <param name="bs"></param>
        /// <returns></returns>
        public static uint Bytes2UInt(byte[] bs)
        {
            return (Bytes2UInt(bs, 0));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bs"></param>
        /// <param name="txt"></param>
        public static void DumpBytes(byte[] bs, string txt)
        {
            System.IO.StreamWriter sw = new System.IO.StreamWriter(txt);
            for (int i = 0; i < bs.Length; i++)
            {
                byte b = bs[i];
                sw.WriteLine(b.ToString("X") + " ");
            }
            sw.WriteLine("-----" + DateTime.Now.ToLocalTime());
            sw.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bs"></param>
        /// <param name="txt"></param>
        public static void DebugString(string bs, string txt)
        {
            System.IO.StreamWriter sw = new System.IO.StreamWriter(txt);
            sw.WriteLine(bs);
            sw.WriteLine("-----" + DateTime.Now.ToLocalTime());
            sw.Close();
        }
    }
}
